package com.meme.editor.model.sprite2d;

import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import java.util.ListIterator;
import java.util.Vector;

import com.meme.editor.EntityObject;



import com.meme.editor.PropertyValueModification;
import com.meme.editor.PropertyValueModificationList;

import com.meme.editor.property.Property;
import com.meme.editor.property.PropertyFactory;
import com.meme.editor.property.value.StringPropertyValue;
import com.meme.io.editor.FileCache;
import com.meme.util.FileUtil;

import com.meme.editor.property.control.FileChooserPropertyControl;

import java.awt.Graphics;

/**
 * Currently, palettes are not supported.
 * 
 * For paletted images, the number of color entries is not limited under 256. 
 * @author normal
 */

public class ModuleImage extends EntityObject
{
	static Logger logger = Logger.getLogger (ModuleImage.class);
	
	
	final static String GroupName_ModuleImage = "Module Image";	
	final static String PropertyName_ImagePath = "Image Path"; 
	
	BufferedImage mImage;
	String mImageFilename;

	public ModuleImage ()
	{
		this (null);
	}
	
	public ModuleImage (String name)
	{
		this (name, null);
	}
	
	public ModuleImage (String name, String pathfile)
	{
		super (name != null ? name : ( pathfile == null ? null : FileUtil.getFilenameWithoutExt(pathfile) ) );
		
		// build property manager
		//createProperty(PropertyGroupName_ModuleImage, PropertyName_ImagePath, new StringPropertyValue (PropertyDefaultValue_ImagePath));
		Property property = createPublicProperty (GroupName_ModuleImage, PropertyName_ImagePath, PropertyFactory.ValueType_String );
		property.setControlType (PropertyFactory.ControlType_ChooseFile);
		property.setParameter(FileChooserPropertyControl.Param_FileFilterParams, "Images Files;jpg;gif;png;bmp");
		
		//
		setImageFilename (pathfile);		
	}
	
	
	@Override
	public String getDefaultName ()
	{
		return "Image";
	}
	
	public void setImageFilename (String filename)
	{
		
		mImageFilename = filename;
		
		if ( mImageFilename == null )
			mImage = null;
		else
			mImage = FileCache.getImage(filename);
		
		changePublicPropertyValue(GroupName_ModuleImage, PropertyName_ImagePath, mImageFilename);
	}
	
	// may change
	public String getImageFilename ()
	{
		return mImageFilename;
	}
	
	@Override
	protected void _handleModifyingPropertyValues (PropertyValueModificationList modications)
	{
		// make some handle to avoid name duplicated
		//...
		
		ListIterator<PropertyValueModification> iter = modications.listIterator();
		while ( iter.hasNext() )
		{
			PropertyValueModification mod = iter.next();
			
			if (   mod.mGroupName.equals (GroupName_ModuleImage)
					&& mod.mPropertyName.equals (PropertyName_ImagePath) )
				{
					setImageFilename ( (String)mod.mNewValue );				
					iter.remove();
				}
		}
		

			
		super._handleModifyingPropertyValues (modications);
	}
	
	@Override
	public boolean isValid ()
	{
		return mImage != null;
	}
	
	
	public int getWidth ()
	{
		if ( isValid () )
			return mImage.getWidth (null);
		else
			return 0;
	}
	
	public int getHeight ()
	{
		if ( isValid () )
			return mImage.getHeight (null);
		else
			return 0;
	}
	
	public BufferedImage getImage ()
	{
		return mImage;
	}
	

//----------------------------------------------------------------------------	
	
//	boolean mPaletted; // false if the number entries is larger than 256
//	int mPalettesCount;
//	Vector<BufferedImage> mPalettedImages;	
//	
//	public ModuleImage (String[] pathfiles)
//	{
//		
//	}
//	
//	public int getPalettesCount ()
//	{
//		return mPalettesCount;
//	}
//	
//	public BufferedImage getImage (int palID)
//	{
//		return mPalettedImages.elementAt (palID);
//	}
}
